package com.aispeech.task.lasr.event;

import com.aispeech.constant.Progress;
import com.aispeech.constant.RedisKeys;
import com.aispeech.data.pojo.task.Task;
import com.aispeech.redis.DbMgr;
import com.aispeech.task.mgr.event.TaskEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LASREvent extends TaskEvent {
    private DbMgr dbMgr = DbMgr.Instance();

    public LASREvent(EventType eventType, Task task) {
        super(eventType, task);
    }

    public LASREvent(Task task) {
        super(task);
    }

    @Override
    public TaskEvent fire() {
        EventType eventType;
        Task task = task();

        switch (eventType()) {
            case Initial:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.DOWNLOADING);
                eventType = doDownload(task);
                break;
            case Downloaded:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.DOWNLOADED);
                eventType = doSplitAudioChannel(task);
                break;
            case ChannelSplitted:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.CHANNEL_SPLITTED);
                eventType = doAudioCut(task);
                break;
            case AudioCutted:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.AUDIOCUTTED);
                eventType = doParallelAsr(task);
                break;
            case Asred:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.ASRED);
                eventType = doFuse(task);
                break;
            case Fused:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.FUSED);
                eventType = doLabel(task);
                break;
            case Labeled:
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.LABELED);
                eventType = doFinish(task);
                dbMgr.hset(RedisKeys.HASH_TASK_PROGRESS, task.getId(), Progress.FINISHED);
                break;
            default:
                eventType = EventType.Error;
                break;
        }

        log.info("[ TaskEvent.fire ]: {} fired. next: {}", this, eventType);

        return new LASREvent(eventType, task());
    }

    private EventType doFinish(Task task) {
        return null;
    }

    private EventType doLabel(Task task) {
        return null;
    }

    private EventType doFuse(Task task) {
        return null;
    }

    private EventType doParallelAsr(Task task) {
        return null;
    }

    private EventType doAudioCut(Task task) {
        return null;
    }

    private EventType doSplitAudioChannel(Task task) {
        return null;
    }

    private EventType doDownload(Task task) {
        EventType eventType = EventType.Error;

        String audioURL = task.getRequest().getAudioFilePath();
        String audioType = task.getRequest().getAudioType().key();



        task.getMetrics().setDownload_t(System.currentTimeMillis());
        eventType = EventType.Downloaded;

        return eventType;
    }
}
